package pack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.ChestionareDAORemote;
import dao.IntrebariDAORemote;
import dao.RaspunsuriCompletateDAORemote;
import dao.RaspunsuriDAORemote;
import dto.ChestionareDTO;
import dto.EmployeeDTO;
import dto.IntrebariDTO;
import dto.RaspunsuriCompletateDTO;
import dto.RaspunsuriDTO;

@ManagedBean(name = "chestionareBean")
@SessionScoped
public class ChestionareBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	ChestionareDAORemote chestionareDAO;

	@EJB
	IntrebariDAORemote intrebariDAO;
	
	@EJB
	RaspunsuriCompletateDAORemote raspunsuriCompletateDAO;

	@EJB
	RaspunsuriDAORemote raspunsuriDAO;

	@ManagedProperty(value = "#{chestionareDTO}")
	private ChestionareDTO chestionareDTO;

	private int id;

	private String nume;

	private String scop;

	private String categorie;

	private String subcategorie;

	private int activ;

	private String dupa;

	private String valoare;

	private ChestionareDTO chestionareUpd;

	List<ChestionareDTO> chestionare;

	List<IntrebariDTO> intrebari;

	List<RaspunsuriDTO> raspunsuri;

	List<RaspunsuriDTO> rasp;

	private String selected;

	private boolean select;

	public void verify() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 5) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		filtru();
		// read();
	}

	public String filtru() {
		readEmp();
		if ("Categorie".equals(getDupa()))
			setChestionare(chestionareDAO.getChestionareDupa(getDupa(),
					getValoare()));
		else if ("Subcategorie".equals(getDupa()))
			setChestionare(chestionareDAO.getChestionareDupa(getDupa(),
					getValoare()));
		else if ("Scop".equals(getDupa()))
			setChestionare(chestionareDAO.getChestionareDupa(getDupa(),
					getValoare()));
		return "";
	}

	public void verifyA() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 1) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}

	public void resetFields() {
		setCategorie("");
		setNume("");
		setSubcategorie("");
		setScop("");
	}

	public String read() {
		chestionare = chestionareDAO.findAllChestionare();
		this.setChestionare(chestionare);
		return "";
	}

	public String readEmp() {
		chestionare = chestionareDAO.findAllChestionareActiv();
		this.setChestionare(chestionare);
		return "";
	}

	public String create() {
		ChestionareDTO chestionareDTO = new ChestionareDTO();
		chestionareDTO.setNume(getNume());
		chestionareDTO.setScop(getScop());
		chestionareDTO.setCategorie(getCategorie());
		chestionareDTO.setSubcategorie(getSubcategorie());
		chestionareDTO.setActiv(1);
		chestionareDAO.create(chestionareDTO);
		resetFields();
		return "";
	}

	public String delete(ChestionareDTO chestionare) {
		chestionareDAO.delete(chestionare);

		return "";
	}

	public String edit(ChestionareDTO chestionare) {
		setChestionareUpd(chestionare);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionare);
		intrebari = intrebariDAO.getIntrebariByChestionar(chestionare);
		this.setIntrebari(intrebari);
		return "chestionareUpdate.xhtml";
	}

	public String addInt(ChestionareDTO chestionare) {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionare);
		setChestionareUpd(chestionare);
		return "intrebariCreate.xhtml";
	}

	public String actInt(ChestionareDTO chestionare) {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionare);
		setChestionareUpd(chestionare);
		return "intrebariUpdate.xhtml";
	}

	public String update() {
		chestionareDAO.update(chestionareUpd);
		return "";
	}

	public String addIntrebare(ChestionareDTO chestionare) {
		setChestionareUpd(chestionare);
		return "chestionareAddIntrebare";
	}

	public String completeaza(ChestionareDTO chestionar) {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionar);
		raspunsuri = new ArrayList<RaspunsuriDTO>();
		setIntrebari(intrebariDAO.getIntrebariByChestionar(chestionar));
		for (int i = 0; i < intrebari.size(); i++) {
			List<RaspunsuriDTO> rasp = new ArrayList<RaspunsuriDTO>();
			rasp = raspunsuriDAO.getRaspunsuriByIntrebare(intrebari.get(i));
			for (int j = 0; j < rasp.size(); j++)
				raspunsuri.add(rasp.get(j));
		}

		return "completeazaChestionar.xhtml";
	}
	
	/* Na szoval: ha mar egyszer kitoltotte, akkor meg kell vizsgalni elozoleg, hogy mar kitoltotte-e az illeto
	 * azaz, megvizsgalom, hogy van-e olyan valasz a DB-ben, aminek az az id-ja amelyik a user id...
	 * ha van, akkor meg sem jelenitem a tablazatban...na szoval ezt a bejelentkezes utan kell megcsinalni
	 * amikor megnyomja, h chestionar...
	 * 
	 * A masik dolog...az adminnak kell csinalni egy olyat, hogy csinalhasson dolgokat a valaszokkal...ezen
	 * meg gondolkodunk majd:D
	 * 
	 * be kell valahogy integralni az apache SHIRO-t!!!! holnap csinald meg te bumbi:D
	 */
	public String salveaza() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ChestionareDTO c  = new ChestionareDTO();
		c = (ChestionareDTO)fc.getExternalContext().getSessionMap()
				.get("chestionarul");
		EmployeeDTO e = new EmployeeDTO();
		e = (EmployeeDTO)fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		int chestionarId = c.getId();
		int angajatId = e.getId();
		for (int i = 0; i < getIntrebari().size(); i++) {
			if (getIntrebari().get(i).getTip() == 1) {
				int intrebareId = getIntrebari().get(i).getId();
				String raspuns = getIntrebari().get(i).getRaspuns();
				RaspunsuriCompletateDTO rcDTO = new RaspunsuriCompletateDTO();
				rcDTO.setChestionarId(chestionarId);
				rcDTO.setIntrebareId(intrebareId);
				rcDTO.setRaspuns(raspuns);
				rcDTO.setAngajatId(angajatId);
				rcDTO.setAngajatName(e.getFirstname() + " " + e.getLastname());
				raspunsuriCompletateDAO.add(rcDTO);
			}
			if (getIntrebari().get(i).getTip() == 2) {
				for (int j = 0; j < getIntrebari().get(i).getRaspunsuri().size(); j++){
					if (getIntrebari().get(i).getSelectat() == getIntrebari().get(i).getRaspunsuri().get(j).getId()){
						int intrebareId = getIntrebari().get(i).getId();
						String raspuns = getIntrebari().get(i).getRaspunsuri().get(j).getRaspuns();
						RaspunsuriCompletateDTO rcDTO = new RaspunsuriCompletateDTO();
						rcDTO.setChestionarId(chestionarId);
						rcDTO.setIntrebareId(intrebareId);
						rcDTO.setRaspuns(raspuns);
						rcDTO.setAngajatId(angajatId);
						rcDTO.setAngajatName(e.getFirstname() + " " + e.getLastname());
						raspunsuriCompletateDAO.add(rcDTO);
					}
				}
				
			}
			if (getIntrebari().get(i).getTip() == 3) {
				for (int j = 0; j < getIntrebari().get(i).getRaspunsuri()
						.size(); j++) {
					if (getIntrebari().get(i).getRaspunsuri().get(j)
							.getSelectat() == true){
						int intrebareId = getIntrebari().get(i).getId();
						String raspuns = getIntrebari().get(i).getRaspunsuri().get(j).getRaspuns();
						RaspunsuriCompletateDTO rcDTO = new RaspunsuriCompletateDTO();
						rcDTO.setChestionarId(chestionarId);
						rcDTO.setIntrebareId(intrebareId);
						rcDTO.setRaspuns(raspuns);
						rcDTO.setAngajatId(angajatId);
						rcDTO.setAngajatName(e.getFirstname() + " " + e.getLastname());
						raspunsuriCompletateDAO.add(rcDTO);
					}
					
				}
			}
		}
		return "chestionare";
	}

	public String setRasp(IntrebariDTO intrebare) {
		setRaspunsuri(raspunsuriDAO.getRaspunsuriByIntrebare(intrebare));
		return "";
	}

	public ChestionareDTO getChestionareDTO() {
		return chestionareDTO;
	}

	public void setChestionareDTO(ChestionareDTO chestionareDTO) {
		this.chestionareDTO = chestionareDTO;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNume() {
		return nume;
	}

	public void setNume(String nume) {
		this.nume = nume;
	}

	public String getScop() {
		return scop;
	}

	public void setScop(String scop) {
		this.scop = scop;
	}

	public String getCategorie() {
		return categorie;
	}

	public void setCategorie(String categorie) {
		this.categorie = categorie;
	}

	public String getSubcategorie() {
		return subcategorie;
	}

	public void setSubcategorie(String subcategorie) {
		this.subcategorie = subcategorie;
	}

	public int getActiv() {
		return activ;
	}

	public void setActiv(int activ) {
		this.activ = activ;
	}

	public List<ChestionareDTO> getChestionare() {
		return chestionare;
	}

	public void setChestionare(List<ChestionareDTO> chestionare) {
		this.chestionare = chestionare;
	}

	public ChestionareDTO getChestionareUpd() {
		return chestionareUpd;
	}

	public void setChestionareUpd(ChestionareDTO chestionareUpd) {
		this.chestionareUpd = chestionareUpd;
	}

	public String getDupa() {
		return dupa;
	}

	public void setDupa(String dupa) {
		this.dupa = dupa;
	}

	public String getValoare() {
		return valoare;
	}

	public void setValoare(String valoare) {
		this.valoare = valoare;
	}

	public List<IntrebariDTO> getIntrebari() {
		return intrebari;
	}

	public void setIntrebari(List<IntrebariDTO> intrebari) {
		this.intrebari = intrebari;
	}

	public List<RaspunsuriDTO> getRaspunsuri() {
		return raspunsuri;
	}

	public void setRaspunsuri(List<RaspunsuriDTO> raspunsuri) {
		this.raspunsuri = raspunsuri;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public boolean getSelect() {
		return select;
	}

	public void setSelect(boolean select) {
		this.select = select;
	}

}
